import java.util.*;

class Team implements Comparable<Team> {
    private String name; // 队名
    private int gamesPlayed; // 已比赛的场数
    private int wins; // 赢的场数
    private int draws; // 平的场数
    private int losses; // 负的场数
    private int goalsScored; // 进球数
    private int goalsConceded; // 失球数
    private int points; // 积分

    public Team(String name) {
        this.name = name;
        this.gamesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsScored = 0;
        this.goalsConceded = 0;
        this.points = 0;
    }

    public void updateMatchResult(int scored, int conceded) {
        this.gamesPlayed++;
        this.goalsScored += scored;
        this.goalsConceded += conceded;

        if (scored > conceded) {
            this.wins++;
            this.points += 3;
        } else if (scored == conceded) {
            this.draws++;
            this.points += 1;
        } else {
            this.losses++;
        }
    }

    public int getGoalDifference() {
        return goalsScored - goalsConceded;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Team other) {
        // 按积分排序
        if (this.points != other.points) {
            return other.points - this.points;
        }
        // 按净胜球排序
        if (this.getGoalDifference() != other.getGoalDifference()) {
            return other.getGoalDifference() - this.getGoalDifference();
        }
        // 按进球数排序
        if (this.goalsScored != other.goalsScored) {
            return other.goalsScored - this.goalsScored;
        }
        return 0;
    }

    @Override
    public String toString() {
        return String.format("%-15s %2d %2d %2d %2d %3d %3d %3d",
                name, gamesPlayed, wins, draws, losses, goalsScored, goalsConceded, points);
    }
}

class League {
    private List<Team> teams;

    public League() {
        teams = new ArrayList<>();
    }

    public void addTeam(String name) {
        teams.add(new Team(name));
    }

    public Team getTeamByName(String name) {
        for (Team team : teams) {
            if (team.getName().equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null;
    }

    public void recordMatch(String homeTeamName, String awayTeamName, int homeScore, int awayScore) {
        Team homeTeam = getTeamByName(homeTeamName);
        Team awayTeam = getTeamByName(awayTeamName);

        if (homeTeam == null || awayTeam == null) {
            System.out.println("错误：无效的球队名称。");
            return;
        }

        homeTeam.updateMatchResult(homeScore, awayScore);
        awayTeam.updateMatchResult(awayScore, homeScore);
    }

    public void printStandings() {
        Collections.sort(teams);
        System.out.println(String.format("%-15s %2s %2s %2s %2s %3s %3s %3s",
                "球队", "场次", "胜", "平", "负", "进球", "失球", "积分"));
        for (Team team : teams) {
            System.out.println(team);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        League league = new League();

        // 添加球队
        System.out.println("请输入球队数量：");
        int numTeams = scanner.nextInt();
        scanner.nextLine(); // 清除换行符

        System.out.println("请输入球队名称：");
        for (int i = 0; i < numTeams; i++) {
            String teamName = scanner.nextLine();
            league.addTeam(teamName);
        }

        // 录入比赛结果
        System.out.println("请输入比赛场数：");
        int numMatches = scanner.nextInt();
        scanner.nextLine(); // 清除换行符

        System.out.println("请输入比赛结果，格式为：主队 客队 主队得分 客队得分");
        for (int i = 0; i < numMatches; i++) {
            String homeTeam = scanner.next();
            String awayTeam = scanner.next();
            int homeScore = scanner.nextInt();
            int awayScore = scanner.nextInt();
            league.recordMatch(homeTeam, awayTeam, homeScore, awayScore);
        }

        // 输出排名
        System.out.println("\n联赛排名：");
        league.printStandings();

        scanner.close();
    }
}